﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Diagnostics;


namespace WickedFlame.GenDock
{
    internal class DockingManagerService
    {
        DockingLayoutManager _manager;

        public DockingManagerService(DockingLayoutManager manager)
        {
            _manager = manager;
        }

        public void ReleaseDockedWindow(DockableWindow wnd)
        {
            ContentDocument content = wnd.Content as ContentDocument;
            if (content != null)
            {
                if (Manager.LeftAnchorType == content.AnchorPlace && Manager.LeftAnchor is IDockableWindowPresenter)
                {
                    //remove from anchor
                    ((IDockableWindowPresenter)Manager.LeftAnchor).ReleaseDockedWindow(wnd);
                }
                else if (Manager.RightAnchorType == content.AnchorPlace && Manager.RightAnchor is IDockableWindowPresenter)
                {
                    //remove from anchor
                    ((IDockableWindowPresenter)Manager.RightAnchor).ReleaseDockedWindow(wnd);
                }
            }
        }

        public void OnDragStart(DockableWindow wnd)
        {
        }

        public void OnDragMove(DockableWindow wnd, Point point)
        {
            if (_manager.RightAnchorRectangle.Contains(point))
            {
            }
        }

        public void OnDrop(DockableWindow wnd, Point point)
        {
            ContentDocument content = wnd.Content as ContentDocument;
            
            if (content != null)
            {
                if (_manager.RightAnchorRectangle.Contains(point))
                {
                    content.AnchorPlace = _manager.RightAnchorType;
                    //wnd.RemoveContent(content);
                    //_manager.RightAnchor.AddContent(content);
                    wnd.Close();

                    _manager.RightAnchor.AddContent(content);
                }
                else if (_manager.LeftAnchorRectangle.Contains(point))
                {
                    content.AnchorPlace = _manager.LeftAnchorType;
                    //wnd.RemoveContent(content);
                    //_manager.LeftAnchor.AddContent(content);
                    wnd.Close();

                    _manager.LeftAnchor.AddContent(content);
                }
            }
        }

        public bool OnDrop(ContentDocument content, Point point)
        {
            bool handled = false;
            
            if (content != null)
            {
                if (content.Parent is DockableWindow)
                {
                    (content.Parent as DockableWindow).Close();
                }

                if (_manager.RightAnchorRectangle.Contains(point))
                {
                    content.AnchorPlace = _manager.RightAnchorType;
                    //_manager.RightAnchor.AddContent(content);
                    _manager.RightAnchor.AddContent(content, point);

                    handled = true;
                }
                else if (_manager.LeftAnchorRectangle.Contains(point))
                {
                    content.AnchorPlace = _manager.LeftAnchorType;
                    //_manager.LeftAnchor.AddContent(content);
                    _manager.LeftAnchor.AddContent(content, point);

                    handled = true;
                }

                //create window
                if (!handled)
                {
                    content.AnchorPlace = AnchorType.None;
                    content.Visibility = Visibility.Visible;

                    var window = new DockableWindow();
                    window.AddContent(content);
                    window.Owner = Window.GetWindow(Manager);

                    window.Title = content.Title;
                    window.IsDocked = false;

                    //set position of window
                    //Point p = System.Windows.Input.Mouse.GetPosition(Window.GetWindow(Manager));
                    //window.Top = p.Y + (window.ActualWidth / 2);
                    //window.Left = p.X + 5;
                    //TODO:
                    //set window to cusor position
                    window.Top = point.Y + (content.ActualWidth / 2);
                    window.Left = point.X + 5;

                    window.Show();
                    handled = true;
                }
            }

            return handled;
        }

        

        internal DockingLayoutManager Manager
        {
            get
            {
                return _manager;
            }
        }
    }
}
